package com.xc.service.impl;

import com.xc.dto.ResultDTO;
import com.xc.entity.NavEntity;
import com.xc.entity.RoleNavEntity;
import com.xc.entity.RoleNavManyEntity;
import com.xc.mapper.NavMapper;
import com.xc.mapper.RoleNavMapper;
import com.xc.service.NavService;
import com.xc.service.RoleNavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleNavServiceImpl implements RoleNavService {
    @Autowired
    private RoleNavMapper roleNavMapper;


    /**
     * 如果传递过来的角色id为0，表示新增多条数据
     *
     * @param roleNavEntity
     * @return
     */
    @Override
    public ResultDTO add(RoleNavEntity roleNavEntity) {
        //增加之前将当前角色关联的权限全部删除

        if (roleNavEntity.getRoleId() == 0) {
            List<Integer> ids = new ArrayList<>();
            ids.add(roleNavEntity.getList().get(0).getRoleId());
            roleNavMapper.deleteByRoleId(ids);
            return addMany(roleNavEntity);
        }
        roleNavMapper.delete(roleNavEntity);
        return ResultDTO.builder()
                .code(0)
                .count(roleNavMapper.add(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO update(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(roleNavMapper.update(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO delete(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(roleNavMapper.delete(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO updateEnable(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .count(roleNavMapper.updateEnable(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO findById(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .code(0)
                .data(roleNavMapper.findById(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO findAll(RoleNavEntity roleNavEntity) {

        return ResultDTO.builder()
                .code(0)
                .code(0)
                .count(roleNavMapper.count(roleNavEntity))
                .data(roleNavMapper.findAll(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO count(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .code(0)
                .count(roleNavMapper.count(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO findByName(RoleNavEntity roleNavEntity) {
        return null;
    }

    @Override
    public ResultDTO addMany(RoleNavEntity roleNavEntity) {
        return ResultDTO.builder()
                .code(0)
                .code(0)
                .count(roleNavMapper.addMany(roleNavEntity))
                .build();
    }

    @Override
    public ResultDTO add(List<RoleNavManyEntity> list) {
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ids.add(list.get(i).getRoleId());
        }
        if (ids.size() > 0) {
            roleNavMapper.deleteByRoleId(ids);
        }


        return ResultDTO.builder()
                .code(0)
                .count(roleNavMapper.addList(list))
                .build();
    }
}
